﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.Security.Cryptography;
using System.Text;
namespace MTGScore_Cards
{


    public static class Storage
    {

        #region Crack

        public static Byte[] CreateMD5Hash(string input)
        {
            var hashalg = new SHA256Managed();
            var h = hashalg.ComputeHash(Encoding.UTF8.GetBytes(input));
            return h;
        }

        public static bool CheckSavedCrack()
        {
            Byte[] saved = GetCrackFile(true);
            Byte[] hash = App.Crack_Hash;
            if (saved.Length != hash.Length)
                return false;
            for (int i = 0; i < saved.Length; i++)
                if (saved[i] != hash[i])
                    return false;
            return true;

        }

        public static bool CheckCrack(Byte[] hash)
        {
            Byte[] saved = GetCrackFile(false);
            if (saved.Length != hash.Length)
                return false;
            for (int i = 0; i < saved.Length; i++)
                if (saved[i] != hash[i])
                    return false;
            SaveCrackFile(saved);
            return true;
        }


        private static Func<StreamReader, Byte[]> read_crack_file = reader =>
        {


            var s = reader.ReadToEnd();
            var a = s.Split(new[] { ';' });
            Byte[] h = new Byte[a.Length];
            for (int i = 0; i < a.Length; i++)
                h[i] = Byte.Parse(a[i]);
            return h;

        };

        private static Action<StreamWriter, Byte[]> write_crack_file = (writer, h) =>
        {
            var s = "";
            for (int i = 0; i < h.Length - 1; i++)
                s += h[i] + ";";
            if (h.Length > 0)
                s += h[h.Length - 1];
            writer.Write(s);
        };

        public static void DeleteCrackFile()
        {
            try
            {
                using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    appStorage.DeleteFile("crack.txt");
                }
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        public static void SaveCrackFile(Byte[] h)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                appStorage.DeleteFile("crack.txt");
                using (var streamFile = appStorage.OpenFile("crack.txt", FileMode.OpenOrCreate))
                using (var writer = new StreamWriter(streamFile))
                    write_crack_file(writer, h);
            }
        }

        public static Byte[] GetCrackFile(bool initial_check)
        {
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var streamFile = appStorage.OpenFile("crack.txt", FileMode.OpenOrCreate))
                {
                    try
                    {
                        using (var reader = new StreamReader(streamFile))
                            return read_crack_file(reader);
                    }
                    catch (Exception)
                    {
                        //SaveCrackFile(StateManager.Crack_Hash);
                        if (initial_check)
                            return new Byte[0];
                        else
                            return App.Crack_Hash;
                    }
                }
            }
        }

        #endregion

        
    }
}
